using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using iBeauty.BusinessObjects;

namespace iBeauty.DAL
{
	public class AccountDAO
	{
		public AccountDAO()
		{
			DbProviderHelper.GetConnection();
		}
		public List<Account> GetAccounts()
		{
			try
			{
				List<Account> lstAccounts = new List<Account>();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [ID],[CreateDate],[ModifiedDate],[TimeStamp],[UserName],[Password],[Permit],[Note] FROM [Account]",CommandType.Text);
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					Account oAccount = new Account();
					oAccount.ID = Convert.ToString(oDbDataReader["ID"]);

					if(oDbDataReader["CreateDate"] != DBNull.Value)
						oAccount.CreateDate = Convert.ToDateTime(oDbDataReader["CreateDate"]);

					if(oDbDataReader["ModifiedDate"] != DBNull.Value)
						oAccount.ModifiedDate = Convert.ToDateTime(oDbDataReader["ModifiedDate"]);

					if(oDbDataReader["TimeStamp"] != DBNull.Value)
						oAccount.TimeStamp = Convert.ToDateTime(oDbDataReader["TimeStamp"]);

					if(oDbDataReader["UserName"] != DBNull.Value)
						oAccount.UserName = Convert.ToString(oDbDataReader["UserName"]);

					if(oDbDataReader["Password"] != DBNull.Value)
						oAccount.Password = Convert.ToString(oDbDataReader["Password"]);

					if(oDbDataReader["Permit"] != DBNull.Value)
						oAccount.Permit = Convert.ToString(oDbDataReader["Permit"]);

					if(oDbDataReader["Note"] != DBNull.Value)
						oAccount.Note = Convert.ToString(oDbDataReader["Note"]);
					lstAccounts.Add(oAccount);
				}
				oDbDataReader.Close();
				return lstAccounts;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public Account GetAccount(string ID)
		{
			try
			{
				Account oAccount = new Account();
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("SELECT [ID],[CreateDate],[ModifiedDate],[TimeStamp],[UserName],[Password],[Permit],[Note] FROM [Account] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				DbDataReader oDbDataReader = DbProviderHelper.ExecuteReader(oDbCommand);
				while (oDbDataReader.Read())
				{
					oAccount.ID = Convert.ToString(oDbDataReader["ID"]);

					if(oDbDataReader["CreateDate"] != DBNull.Value)
						oAccount.CreateDate = Convert.ToDateTime(oDbDataReader["CreateDate"]);

					if(oDbDataReader["ModifiedDate"] != DBNull.Value)
						oAccount.ModifiedDate = Convert.ToDateTime(oDbDataReader["ModifiedDate"]);

					if(oDbDataReader["TimeStamp"] != DBNull.Value)
						oAccount.TimeStamp = Convert.ToDateTime(oDbDataReader["TimeStamp"]);

					if(oDbDataReader["UserName"] != DBNull.Value)
						oAccount.UserName = Convert.ToString(oDbDataReader["UserName"]);

					if(oDbDataReader["Password"] != DBNull.Value)
						oAccount.Password = Convert.ToString(oDbDataReader["Password"]);

					if(oDbDataReader["Permit"] != DBNull.Value)
						oAccount.Permit = Convert.ToString(oDbDataReader["Permit"]);

					if(oDbDataReader["Note"] != DBNull.Value)
						oAccount.Note = Convert.ToString(oDbDataReader["Note"]);
				}
				oDbDataReader.Close();
				return oAccount;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddAccount(string ID,DateTime?CreateDate,DateTime?ModifiedDate,DateTime?TimeStamp,string UserName,string Password,string Permit,string Note)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [Account]([CreateDate],[ModifiedDate],[TimeStamp],[UserName],[Password],[Permit],[Note])VALUES(@CreateDate,@ModifiedDate,@TimeStamp,@UserName,@Password,@Permit,@Note)",CommandType.Text);
				if (CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (UserName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,UserName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,DBNull.Value));
				if (Password!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,Password));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,DBNull.Value));
				if (Permit!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,Permit));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,DBNull.Value));
				if (Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int AddAccount(Account oAccount)
		{
			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("INSERT INTO [Account]([CreateDate],[ModifiedDate],[TimeStamp],[UserName],[Password],[Permit],[Note])VALUES(@CreateDate,@ModifiedDate,@TimeStamp,@UserName,@Password,@Permit,@Note)",CommandType.Text);
				if (oAccount.CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,oAccount.CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (oAccount.ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,oAccount.ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (oAccount.TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,oAccount.TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (oAccount.UserName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,oAccount.UserName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,DBNull.Value));
				if (oAccount.Password!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,oAccount.Password));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,DBNull.Value));
				if (oAccount.Permit!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,oAccount.Permit));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,DBNull.Value));
				if (oAccount.Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,oAccount.Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));

				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateAccount(string ID,DateTime?CreateDate,DateTime?ModifiedDate,DateTime?TimeStamp,string UserName,string Password,string Permit,string Note)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [Account] SET [CreateDate]=@CreateDate,[ModifiedDate]=@ModifiedDate,[TimeStamp]=@TimeStamp,[UserName]=@UserName,[Password]=@Password,[Permit]=@Permit,[Note]=@Note WHERE [ID]=@ID",CommandType.Text);
				if (CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (UserName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,UserName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,DBNull.Value));
				if (Password!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,Password));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,DBNull.Value));
				if (Permit!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,Permit));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,DBNull.Value));
				if (Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int UpdateAccount(Account oAccount)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("UPDATE [Account] SET [CreateDate]=@CreateDate,[ModifiedDate]=@ModifiedDate,[TimeStamp]=@TimeStamp,[UserName]=@UserName,[Password]=@Password,[Permit]=@Permit,[Note]=@Note WHERE [ID]=@ID",CommandType.Text);
				if (oAccount.CreateDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,oAccount.CreateDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@CreateDate",DbType.DateTime,DBNull.Value));
				if (oAccount.ModifiedDate.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,oAccount.ModifiedDate));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ModifiedDate",DbType.DateTime,DBNull.Value));
				if (oAccount.TimeStamp.HasValue)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,oAccount.TimeStamp));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@TimeStamp",DbType.DateTime,DBNull.Value));
				if (oAccount.UserName!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,oAccount.UserName));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@UserName",DbType.String,DBNull.Value));
				if (oAccount.Password!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,oAccount.Password));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Password",DbType.String,DBNull.Value));
				if (oAccount.Permit!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,oAccount.Permit));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Permit",DbType.String,DBNull.Value));
				if (oAccount.Note!=null)
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,oAccount.Note));
				else
					oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@Note",DbType.String,DBNull.Value));
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,oAccount.ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveAccount(Account oAccount)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Account] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,oAccount.ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		public int RemoveAccount(string ID)
		{

			try
			{
				DbCommand oDbCommand = DbProviderHelper.CreateCommand("DELETE FROM [Account] WHERE [ID]=@ID",CommandType.Text);
				oDbCommand.Parameters.Add(DbProviderHelper.CreateParameter("@ID",DbType.String,ID));
				return DbProviderHelper.ExecuteNonQuery(oDbCommand);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
	}
}
